package com.xjm.ips.utils;

import com.xjm.ips.entity.SettleSalary;
import com.xjm.ips.entity.SettleTax;

import java.util.Calendar;

public class SalaryUtils {

    /**
     * 计算当月税后工资
     * @param settleSalary 工资结算类，传入的数据里面有当前税前工资，五险一金，罚款等信息
     * @param sumSalary 累计至上个月的预扣缴额
     * @param sumTax 累计至上个月的税金
     * @return
     */
    public static SettleTax calcMonthSalary(SettleSalary settleSalary, Double sumSalary, Double sumTax){
        SettleTax settleTax = new SettleTax();
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;//当前月份
        Double currentSalary = settleSalary.getAllsalary();//当月税前工资
        Double socialInsurance = settleSalary.getSocialInsurance();//五险一金
        Double lateMoney = settleSalary.getLateMoney();//迟到罚金
        Double takeLeaveMoney = settleSalary.getTakeLeaveMoney();//请假罚金
        //扣除完、起征点、罚款及五险一金后的纳税额度
        Double actrualTaxSalary = currentSalary-lateMoney-takeLeaveMoney-5000;
        //累计预扣预缴应纳税所得额
        Double currentActrualTaxSalary = actrualTaxSalary+sumSalary;
        //税金
        Double taxMoney = getCurrentMonthTax(currentActrualTaxSalary,sumTax);
        settleTax.setTaxMoney(taxMoney);
        settleTax.setActrualTaxSalary(actrualTaxSalary);
        settleTax.setActualIncome(currentSalary-lateMoney-takeLeaveMoney-taxMoney);
        return settleTax;
    }

    /**
     * 获取本月应缴税金:
     * 本月应缴税金=累计纳税额度*区间利率-累计至上个月的税金
     * @param actrualTaxSalary 累计纳税额度
     * @param sumTax 累计至上个月的税金
     * @return
     */
    public static  Double getCurrentMonthTax(Double actrualTaxSalary,Double sumTax){
        Double result = 0.0;//累计纳税额度*区间利率
        if (actrualTaxSalary <= 36000.0) {
            result = actrualTaxSalary * 0.03;
        } else if (actrualTaxSalary <= 144000.0) {
            result = actrualTaxSalary * 0.1 - 2520;
        } else if (actrualTaxSalary <= 300000.0) {
            result = actrualTaxSalary * 0.2 - 16920;
        } else if (actrualTaxSalary <= 420000.0) {
            result = actrualTaxSalary * 0.25 - 31920;
        } else if (actrualTaxSalary <= 660000.0) {
            result = actrualTaxSalary * 0.30 - 52920;
        } else if (actrualTaxSalary <= 960000.0) {
            result = actrualTaxSalary * 0.35 - 85920;
        } else {
            result = actrualTaxSalary * 0.45 - 181920;
        }
        return result-sumTax;
    }
}
